package com.weitu.hiddenDanger.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.weitu.hiddenDanger.entity.GovernMonReport;
import com.weitu.hiddenDanger.mapper.HDGovernMapper;
import com.weitu.hiddenDanger.mapper.HDRegisterMapper;
import com.weitu.hiddenDanger.service.HDGovernService;
import com.weitu.util.IDGenerator;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by fanyuting on 2017/4/14 0014.
 */
@Service
public class HDGovernServiceImpl implements HDGovernService{
    @Autowired
    private HDGovernMapper hdGovernMapper;
    @Autowired
    private HDRegisterMapper hdRegisterMapper;
    /**
     * 查询隐患月报列表
     * @param map
     * @return
     */
    @Override
    public List<GovernMonReport> queryGovernMonList(Map<String, Object> map) {
        return hdGovernMapper.queryGovernMonList(map);
    }

    /**
     * 查询隐患月报列表总数
     * @param map
     * @return
     */
    @Override
    public int queryGovernMonListCount(Map<String, Object> map) {
        return hdGovernMapper.queryGovernMonListCount(map);
    }

    /**
     * 隐患治理分页列表查询
     * @param map
     * @return
     */
    public List<Map<String,Object>> queryGovernList(Map<String,Object> map){
        return hdGovernMapper.queryGovernList(map);
    }

    /**
     * 查询隐患治理分页列表总数
     * @param map
     * @return
     */
    public int queryGovernListCount(Map<String,Object> map){
        return hdGovernMapper.queryGovernListCount(map);
    }
    //隐患治理分页列表查询（编辑页面）
    public List<Map<String,Object>> queryEditPageGovernList(Map<String,Object> map){
        return hdGovernMapper.queryEditPageGovernList(map);
    }
    //查询隐患治理分页列表总数（编辑页面）
    public int queryEditPageGovernListCount(Map<String,Object> map){
        return hdGovernMapper.queryEditPageGovernListCount(map);
    }

    //根据id查询隐患月报信息
    public GovernMonReport queryGovernMonById(Map<String,Object> map){
        return hdGovernMapper.queryGovernMonById(map);
    }
    /**
     * 新增隐患治理信息
     * @param map
     * @return
     */
    @Override
    @Transactional
    public boolean insertGovernInfo(Map<String, Object> map) {
        String addTime = map.get("addTime").toString();
        String addUserId= map.get("addUserId").toString();
        String addDeptId=map.get("addDeptId").toString();
        String monthReportId = map.get("id").toString();
        String dataStr = null;
        int flag = 0;
        int flag1 =0;
        if(map.get("governListData")!=null){
            dataStr = map.get("governListData").toString();
            //将json字符串转为list对象
            List<Map> list = JSONArray.parseArray(dataStr,Map.class);
            if(list.size()>0){
                //新建list存放治理信息
                List<Map<String,Object>> governList = new ArrayList<Map<String,Object>>();
                List<Map<String,Object>> proressList = new ArrayList<Map<String,Object>>();
                for(Map m:list){
                    String uuid = IDGenerator.getId();
                    String hdId = m.get("id").toString();
                    Map<String,Object> gm = new HashedMap();
                    Map<String,Object> pm = new HashedMap();
                    gm.put("id",uuid);
                    gm.put("hdId",hdId);
                    gm.put("monthReportId",monthReportId);
                    gm.put("addUserId",addUserId);
                    gm.put("addDeptId",addDeptId);
                    gm.put("actualFinishTime",m.get("actualFinishTime"));
                    gm.put("governContent",m.get("governContent"));
                    gm.put("specificProgress",m.get("specificProgress"));
                    gm.put("progressNodeId",m.get("progressNodeId"));
                    gm.put("monthInvest",m.get("monthInvest"));
                    gm.put("allInvest",m.get("allInvest"));
                    pm.put("hdId",hdId);
                    pm.put("progressNodeId",m.get("progressNodeId"));
                    governList.add(gm);
                    proressList.add(pm);
                }
                map.put("addList",governList);
                Map<String,Object> map1 = new HashedMap();
                map1.put("list",proressList);
                flag= this.hdGovernMapper.insertGovernInfo(map);
                flag1 = this.hdRegisterMapper.editHDProgressNode(map1);
            }else{
                flag = 1;
                flag1 = 1;
            }
        }
        //String governId = IDGenerator.getId();
        if(flag<1 ||flag1<1){
            return false;
        }
        return true;
    }
    //编辑隐患治理信息
    public boolean editGovernInfo(Map<String,Object> map){
        String updateTime = map.get("updateTime").toString();
        String updateUserId= map.get("updateUserId").toString();
        String updateDeptId=map.get("updateDeptId").toString();
        String monthReportId = map.get("id").toString();
        String dataStr = null;
        int flag = 0;
        int flag1 = 0;
        if(map.get("governListData")!=null){
            dataStr = map.get("governListData").toString();
            //将json字符串转为list对象
            List<Map> list = JSONArray.parseArray(dataStr,Map.class);
            if(list.size()>0){
                //新建list存放治理信息
                List<Map<String,Object>> governList = new ArrayList<Map<String,Object>>();
                List<Map<String,Object>> proressList = new ArrayList<Map<String,Object>>();
                for(Map m:list){
                    //String uuid = IDGenerator.getId();
                    String hdId = m.get("id").toString();
                    Map<String,Object> gm = new HashedMap();
                    Map<String,Object> pm = new HashedMap();
                    gm.put("id",m.get("governId"));
                    gm.put("monthReportId",monthReportId);
                    gm.put("addUserId",updateUserId);
                    gm.put("addDeptId",updateDeptId);
                    gm.put("actualFinishTime",m.get("actualFinishTime"));
                    gm.put("governContent",m.get("governContent"));
                    gm.put("specificProgress",m.get("specificProgress"));
                    gm.put("progressNodeId",m.get("progressNodeId"));
                    gm.put("monthInvest",m.get("monthInvest"));
                    gm.put("allInvest",m.get("allInvest"));
                    pm.put("hdId",hdId);
                    pm.put("progressNodeId",m.get("progressNodeId"));
                    governList.add(gm);
                    proressList.add(pm);
                }
                map.put("updateList",governList);
                Map<String,Object> map1 = new HashedMap();
                map1.put("list",proressList);
                flag= this.hdGovernMapper.editGovernInfo(map);
                flag1 = this.hdRegisterMapper.editHDProgressNode(map1);
            }else{
                flag = 1;
                flag1 =1;
            }
        }
        //String governId = IDGenerator.getId();
        if(flag<1||flag1<1){
            return false;
        }
        return true;
    }
    /**
     * 新增隐患治理月报信息
     * @param map
     * @return
     */
    @Override
    public boolean insertGovernMonReport(Map<String, Object> map) {
        int flag = this.hdGovernMapper.insertGovernMonReport(map);
        if(flag<1){
            return false;
        }
        return true;
    }
    /**
     * 新增隐患治理月报信息
     * @param map
     * @return
     */
    @Override
    public boolean editGovernMonReport(Map<String, Object> map) {
        int flag = this.hdGovernMapper.editGovernMonReport(map);
        if(flag<1){
            return false;
        }
        return true;
    }
    //删除隐患治理月报
    public boolean deleteGovernMonReport(Map<String,Object> map){
        int flag = this.hdGovernMapper.deleteGovernMonReport(map);
        if(flag<1){
            return false;
        }
        return true;
    }
}
